package com.example.robertluozizhao.framecollectdemo.fragment;


import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.robertluozizhao.framecollectdemo.R;
import com.example.robertluozizhao.framecollectdemo.view.custom.RcycleViewConflitTest;

/**
 * Created by robert.luozizhao on 2018/3/5.
 */

public class TwoFragment extends Fragment {

    private RcycleViewConflitTest recyclerView;
    private String[] strings = new String[1000];
    private View recycleConflit;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        for (int i = 0; i < strings.length; i++) {
            strings[i] = i + "";
        }
        recycleConflit = inflater.inflate(R.layout.fragment_scroll_text, null);
        return recycleConflit;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        recyclerView = (RcycleViewConflitTest) recycleConflit.findViewById(R.id.rv_diy_test);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(new RecyclerView.Adapter() {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return new ViewHodlerRC(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_test, parent,false));
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                ViewHodlerRC viewHodlerRC = (ViewHodlerRC) holder;
                ((ViewHodlerRC) holder).setString(strings[position]);
            }

            @Override
            public int getItemCount() {
                return strings.length;
            }
        });
    }

    public static class ViewHodlerRC extends RecyclerView.ViewHolder {

        TextView textView;

        public ViewHodlerRC(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_test);
        }

        public void setString(String s) {
            textView.setText(s);
        }
    }


}
